#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    vector<int> suf(n + 1);
    for(int i = n - 1; i >= 0; i--) {
        if(a[i] == 0) {
            suf[i] = suf[i + 1] + 1;
        }else{
            suf[i] = suf[i + 1];
        }
    }
    ll ans = 0, cnt = 0, pre = 0;
    for(int i = 0; i < n; i++) {
        if(a[i] == 1) cnt++;
        else ans += cnt;
    }
    ll t = ans;
    for(int i = 0; i < n; i++) {
        ll tmp = t;
        if(a[i] == 1) {
            tmp += pre;
            tmp -= suf[i + 1];
        }else{
            tmp -= pre;
            tmp += suf[i + 1];
        }
        ans = max(ans, tmp);
        if(a[i] == 1) pre++;
    }
    cout << ans << endl;

}

int main() {
    int t;
    cin >> t;
    while(t--) solve();
    return 0;
}